<?php
/**################################################################################################**   
# Copyright (c)  2008  Carlos Galindo.   
# Permission is granted to copy, distribute and/or modify this document   
# under the terms of the GNU Free Documentation License, Version 1.2   
# or any later version published by the Free Software Foundation;   
# Provided 'as is' with no warranties, nor shall the autor be responsible for any mis-use of the same.     
# A copy of the license is included in the section entitled 'GNU Free Documentation License'.   
#   
#   CJAX FRAMEWORK 3.0.0RC2                $     
#   ajax made easy with cjax                    
#   -- DO NOT REMOVE THIS --                    
#   -- AUTHOR COPYRIGHT MUST REMAIN INTACT -   
#   CJAX FRAMEWORK  3.0.0RC2               $     
#   Written by: Carlos Galindo                  
#   Website: www.cjax.net                     $      
#   Email: cjxxi@msn.com    
#   Date: 2/12/2007                           $     
#   File Last Changed:  03/24/2009            $     
**####################################################################################################*/   

require_once 'cjax/cjax.php';   
class ajax  {     
    function ajax()
    {    define('CJAX_CONTROLLER',1);
        $CJAX = CJAX::getInstance(); 
        $controller = $CJAX->get('controller');
        if(file_exists('controllers/load_controller.php')) {
        	require_once 'controllers/load_controller.php';
        }
        if($CJAX->controller_dir) { 
        	$file = $CJAX->controller_dir.'/controllers/'.$controller.'_cjax.php';;
        } else {
        	 $file = 'controllers/'.$controller.'_cjax.php';
        }
       
        if (!is_file($file)) {
            die("controller file: $file not found");
        }
        require_once $file;
        $class = 'handle_'.$controller;
        if(!class_exists($class)) {
            die("controller class not \"{$class}\" found");
        }
        $section = $CJAX->get('section');
        if(!$section) { 
            die("Method (function) not defined");
        }
        $obj = cjax_singleton::getInstance($class);
        if(!method_exists($obj,$section)) {
            die("controller method/section \"$section\" not found"); 
        }
        $CJAX->controller = $_GET['controller'];
        $CJAX->section = $_GET['section'];
        $confirm = $CJAX->get('confirm');
        if($confirm) {
        	foreach($_GET as $k =>$v) {
        		if($k=='confirm') {
        			continue;
        		}
        		$url[] = "$k=$v";
        	}
        	$file = $CJAX->get('ajax_file');
        	if(!$file) {
        		$file = 'ajax.php';
        	}
        	$url = $CJAX->call_js("$file?".implode("&",$url));
        	#$CJAX->action('cjax_comfirm_yes',$url);
        	
        	$confirm .= "
        	<br />
        	<span style='float: left;'>
        	<a href=\"#\" onclick=\"CJAX.remove('cjax_message')\">No</a>
        	</span>
        	<span style='float: right;'>
        	<a href='#' $url>Yes</a>
        	</span>
        	<div style='clear:both'></div>
        	";

        	
        	$CJAX->message($CJAX->format->message($confirm));
        	
        	
        	exit();
        }
        unset($_GET['controller']);
        unset($_GET['section']);
        $r = $obj->{$section}();
    }
    
}
new ajax;